package com.lzb.compressmaster.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.FileSizeAdapter;
import com.lzb.compressmaster.basic.BaseSmartDialog;
import com.lzb.compressmaster.bean.FileSizeBean;
import com.lzb.compressmaster.databinding.DialogCompressBinding;
import com.lzb.compressmaster.databinding.DialogFileSizeBinding;

import java.util.ArrayList;
import java.util.List;

public class FileSizeDialog extends BaseSmartDialog<DialogFileSizeBinding> implements View.OnClickListener {

    private FileSizeAdapter fileSizeAdapter;
    private int selPosition;

    public FileSizeDialog(@NonNull Context context) {
        super(context);
    }

    public void setFileSizeAdapter(FileSizeAdapter fileSizeAdapter) {
        this.fileSizeAdapter = fileSizeAdapter;
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_file_size;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.rvFileSizeList.setLayoutManager(new LinearLayoutManager(getContext()));
        fileSizeAdapter = new FileSizeAdapter();
        mDataBinding.rvFileSizeList.setAdapter(fileSizeAdapter);
        fileSizeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                fileSizeAdapter.getItem(selPosition).setSelected(false);
                selPosition = position;
                fileSizeAdapter.getItem(position).setSelected(true);
                fileSizeAdapter.notifyDataSetChanged();
                dismiss();
                if (listener != null) {
                    listener.clickConfirm(fileSizeAdapter.getItem(position).getName());
                }
            }
        });

        List<FileSizeBean> list = new ArrayList<>();
        list.add(new FileSizeBean(getContext().getString(R.string.unit_kb), false));
        list.add(new FileSizeBean(getContext().getString(R.string.unit_mb), false));
        list.get(selPosition).setSelected(true);
        fileSizeAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String name);
    }
}
